//
//  BVImageViewController.m
//  BuyVegetables1
//
//  Created by wangbing on 16/6/28.
//  Copyright © 2016年 叶思超. All rights reserved.
//

#import "BVImageViewController.h"
#import "UIImageView+WebCache.h"
#import "BVConst.h"


@interface BVImageViewController ()

{
    UIScrollView *_scrollView;
    UIImageView *imgview;
}
@end

@implementation BVImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = BVBackgroundLightGrayColor;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, BVScreenW, BVScreenH)];
    [self.view addSubview:_scrollView];
     _scrollView.delegate = self;
    _scrollView.maximumZoomScale = 1.5;
    _scrollView.minimumZoomScale = 0.5;
    
    
    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, BVScreenW, BVScreenH)];
    [_scrollView addSubview:imgview];
    
    imgview.userInteractionEnabled = YES;
    imgview.contentMode = UIViewContentModeScaleAspectFit;
    
    [imgview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", Path, self.imgName]]];
    
    
    //创建双击手势
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleClickAction:)];
    //设置点击数量
    tap2.numberOfTapsRequired = 2;
    
    //设置触摸手指的数量
    tap2.numberOfTouchesRequired = 1;
    
    //将手势对象添加到视图上
    [_scrollView  addGestureRecognizer:tap2];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backVC)];
    [_scrollView addGestureRecognizer:tap];
    
    //tap2被触发时，让tap1失效
    [tap requireGestureRecognizerToFail:tap2];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backVC)];

}

- (void)doubleClickAction:(UITapGestureRecognizer *)tap
{
    if (_scrollView.zoomScale > 1) {//大于1，说明当前是放大状态
        
        [_scrollView setZoomScale:1 animated:YES];
    }else{
        
        [_scrollView setZoomScale:1.5 animated:YES];
    }
    
}
//返回需要缩放的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imgview;
}

- (void)backVC{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
